package com.cskaoyan.service.admin;

import com.cskaoyan.bean.*;
import com.cskaoyan.mapper.MarketCouponMapper;
import com.cskaoyan.model.admin.bo.ListBo;
import com.cskaoyan.model.admin.bo.MarketCouponBo;
import com.cskaoyan.model.admin.vo.AdBaseData;
import com.cskaoyan.utils.BaseDataOnListUtil;
import com.cskaoyan.utils.CouponUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class CouponServiceImpl implements CouponService{
    @Autowired
    MarketCouponMapper couponMapper;
    @Override
    public AdBaseData list(ListBo listBo,MarketCoupon marketCoupon) {
        MarketCouponExample marketCouponExample = new MarketCouponExample();

        if(marketCoupon.getName()!=null||marketCoupon.getType()!=null||marketCoupon.getStatus()!=null){
            MarketCouponExample.Criteria criteria = marketCouponExample.createCriteria();
//            criteria.andDeletedEqualTo(0);
            if(marketCoupon.getName()!=null){
                criteria =criteria.andNameLike(CouponUtil.addHundred(marketCoupon.getName()));
            }if(marketCoupon.getType()!=null){
                criteria =criteria.andTypeEqualTo(marketCoupon.getType());
            }if(marketCoupon.getStatus()!=null){
                criteria =criteria.andStatusEqualTo(marketCoupon.getStatus());
            }

        }

        marketCouponExample.setOrderByClause(listBo.getSort()+" "+ listBo.getOrder());
        List<MarketCoupon> marketCoupons = couponMapper.selectByExample(marketCouponExample);

        return BaseDataOnListUtil.getAdBaseData(listBo, marketCoupons);
    }



    @Override
    public MarketCoupon update(MarketCoupon2 marketCoupon2) {
        marketCoupon2.setUpdateTime(new Date());
        int i = couponMapper.updateByPrimaryKeySelective2(marketCoupon2);
        MarketCoupon marketCoupon1 = couponMapper.selectByPrimaryKey(marketCoupon2.getId());
        return marketCoupon1;
    }

    @Override
    public Integer delete(MarketCoupon marketCoupon) {
        int i = couponMapper.deleteByPrimaryKey(marketCoupon.getId());
//        marketCoupon.setDeleted(true);
//        int i = couponMapper.updateByPrimaryKeySelective(marketCoupon);
//        int i=couponMapper.updateDeletedById(marketCoupon.getId());
        return i;
    }

    @Override
    public MarketCoupon create(MarketCouponBo marketCouponBo) {
        marketCouponBo.setAddTime(CouponUtil.dateToString(new Date()));
        marketCouponBo.setUpdateTime(CouponUtil.dateToString(new Date()));
        int i=couponMapper.insertSelective2(marketCouponBo);

        MarketCouponExample couponExample = new MarketCouponExample();
        couponExample.createCriteria().andNameEqualTo(marketCouponBo.getName());

//        marketCouponBo.setAddTime(new Date());
        List<MarketCoupon> marketCoupons = couponMapper.selectByExample(couponExample);
        return null;
    }

//    @Override
//    public MarketCoupon create(MarketCoupon marketCoupon) {
//
//        int i = couponMapper.insertSelective(marketCoupon);
//
//        MarketCouponExample couponExample = new MarketCouponExample();
//        couponExample.createCriteria().andNameEqualTo(marketCoupon.getName());
//
//        marketCoupon.setAddTime(new Date());
//        List<MarketCoupon> marketCoupons = couponMapper.selectByExample(couponExample);
//
//        return marketCoupons.get(0);
//    }

    @Override
    public AdBaseData listuser(ListBo listBo, String couponId) {
        PageHelper.startPage(listBo.getPage(), listBo.getLimit());

        MarketCouponExample marketCouponExample = new MarketCouponExample();

        marketCouponExample.setOrderByClause(listBo.getSort()+" "+ listBo.getOrder());

        MarketCoupon marketCoupon = couponMapper.selectByPrimaryKey(Integer.parseInt(couponId));

        List<MarketCoupon> marketCoupons =new ArrayList<MarketCoupon>(0);
//        marketCoupons.add(marketCoupon);
                PageInfo pageInfo = new PageInfo();
        long total = pageInfo.getTotal();

        Integer pages=null;

        if(listBo.getLimit()!=null&& listBo.getLimit()!=0){

            pages=marketCoupons.size()/ listBo.getLimit();

        }

        return AdBaseData.list(marketCoupons, listBo.getPage(), listBo.getLimit(),total,pages);

    }

    @Override
    public MarketCoupon couponRead(Integer id) {
        MarketCoupon marketCoupon = couponMapper.selectByPrimaryKey(id);
        return marketCoupon;
    }


}
